<template>
  <div v-if="visible" class="mask" @click="closeMask">
    <div class="main" @click.stop="">
      <div style="z-index: 100" class="user-info">
        <el-avatar
          src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png"
        />
        <div class="title" v-if="level === 0">用户Arrebol，您还未开通VIP</div>
        <div class="title" v-else>用户Arrebol，您可以续费VIP</div>
      </div>
      <div class="shop-list not-scrool">
        <div
          class="vip-card"
          v-for="item of props.dataList"
          :key="item.id"
          :class="{ active: shopId === item.id }"
          @click="toPay(item.id)"
        >
          <div>{{ item.desc }}</div>
          <div>
            ￥<span>{{ item.price * item.discount }}</span>
          </div>
          <div>
            {{
              api.getDate(item.price, item.discount, item.date, item.dateUnit)
            }}
          </div>
        </div>
      </div>

      <div v-show="shopId != null" style="padding: 10px 0; margin-left: 10px">
        <el-button
          color="#ebc96a"
          @click="pay"
          style="color: #fff"
          :disabled="isCreate"
          :loading="isCreate"
        >
          立即支付
        </el-button>
      </div>
      <div style="width: 100%; text-align: center">
        <div
          style="
            width: 100px;
            height: 100px;
            display: flex;
            align-items: center;
            justify-content: center;
            margin: 20px 10px;
            background: #eee;
          "
          v-loading="buyLoading"
          element-loading-background="rgba(0,0,0,0.5)"
          v-if="qrCode == null ? false : true"
        >
          <el-image
            style="width: 100px; height: 100px"
            :src="qrCode"
            fit="fill"
          >
            <template #error>
              <div class="image-slot">未加载</div>
            </template>
          </el-image>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, defineExpose, defineProps } from "vue";
import request from "@/assets/js/request";
import { ElMessage } from "element-plus";
import api from "@/assets/js/api";

const props = defineProps({
  dataList: {
    type: Array,
    default: () => [],
  },
});

const visible = ref(false);
const shopId = ref();
const level = ref(0);
const qrCode = ref();
const buyLoading = ref(false);
const isCreate = ref(false);

const toPay = (id) => {
  shopId.value = id;
};

const pay = () => {
  isCreate.value = true;
  buyLoading.value = true;
  request
    .get(`/pay/alipay?shopId=${shopId.value}`)
    .then((res) => {
      if (res.code === 200) {
        qrCode.value = "data:image/png;base64," + res.data;
      }
      buyLoading.value = false;
      isCreate.value = false;
    })
    .catch((err) => {
      ElMessage.error(err.message);
      buyLoading.value = false;
      isCreate.value = false;
    });
};

const open = (item) => {
  visible.value = true;
  level.value = item;
};
const closeMask = () => {
  if (isCreate.value) {
    ElMessage.warning("正在创建支付...");
    return;
  }
  visible.value = false;
};

defineExpose({
  open,
});
</script>

<style scoped lang="scss">
@mixin jiantou() {
  width: 40px;
  text-align: center;
  height: 40px;
  &:hover {
    background: #fae6b8;
    border-radius: 50%;
  }
}
.my-header {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  gap: 16px;
}

.mask {
  background: rgba(0, 0, 0, 0.7);
  width: 100%;
  height: 100%;
  position: fixed;
  left: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  top: 0;
}
.main {
  background-image: linear-gradient(135deg, #fff0d2, #fce59f 100%);
  width: 60%;
  min-height: 400px;
  position: relative;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  &::before {
    content: "VIP";
    font-size: 300px;
    position: absolute;
    top: 0;
    right: 80px;
    color: rgba(#fff, 0.2);
    font-size: 300px;
    z-index: -1;
    transform: rotate(45deg);
  }
  &::after {
    z-index: -1;
    content: "VIP";
    font-size: 300px;
    position: absolute;
    top: 0;
    right: 0;
    color: rgba(#fff, 0.8);
    text-shadow: -2px -4px 15px 12px #fff;
    font-size: 300px;
    transform: rotate(45deg);
  }
}

.shop-list {
  box-sizing: border-box;
  padding: 10px;
  margin-top: 10px;
  overflow-x: auto;
  display: flex;
}

.vip-card {
  background: #fff;
  // display: flex;
  // flex-direction: column;
  // justify-content: center;
  // align-items: center;
  text-align: center;
  width: 8rem;
  height: 10rem;
  border-radius: 5px;
  border: #eee 1px solid;
  padding: 10px;
  margin-right: 10px;
  &:hover {
    cursor: pointer;
  }
  & > div {
    min-height: 2rem;
  }
  & > div:nth-child(1) {
    font-size: 18px;
    font-weight: 600;
    width: 8rem;
  }

  & > div:nth-child(2) {
    color: #dfb349;
    & > span {
      font-size: 30px;
    }
  }
  & > div:nth-child(3) {
    color: #8f8f8f;
    font-size: 14px;
    margin-top: 10px;
  }
}

.user-info {
  display: flex;
  margin-top: 20px;
  box-sizing: border-box;
  padding: 1rem;
}

.title {
  display: flex;
  align-items: center;
  margin-left: 1rem;
}

.active {
  border: #ebc96a 1px solid !important;
  background: #fff9e7 !important;
}

.image-slot {
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}
</style>
import api from "@/assets/js/api";
